package com.ruoyi.framework.web.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GoodLuck;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.pay.service.IPayCoinCashService;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqFriendApply;
import com.ruoyi.wq.domain.vo.WqFriendApplyVO;
import com.ruoyi.wq.service.IWqFriendApplyService;
import com.ruoyi.wx.service.IWxAppService;
import com.ruoyi.wx.service.WxMsgService;

@Component
public class FriendApplyService {

	@Autowired
    private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private IWqFriendApplyService wqFriendApplyService;
	
	@Autowired
    private WxMsgService wxMsgService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private IWxAppService wxAppService;
	
	@Autowired
	private AppConfigService appConfigService;
	
	@Autowired
	private IPayCoinCashService payCoinCashService;
	
	@Autowired
	private RedisCache redisCache;
	
	/**
	 * 升级关系
	 * @param wqFriendApply
	 * @return
	 */
	public Integer updateFriendApplyHot(WqFriendApply wqFriendApply) {
		PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		payCoinUserForm.setUserId(SecurityUtils.getUserId());
		payCoinUserForm.setCoinNum(wqFriendApply.getApplyNum());
		payCoinUserForm.setSourceId(wqFriendApply.getId());
		Integer applyNum = payCoinUserService.saveApply(payCoinUserForm);
		if(applyNum == 0){
			return 0;
		}
		
		WqFriendApply apply = wqFriendApplyService.getById(wqFriendApply.getId());
		apply.setApplyNum(applyNum);
		wqFriendApplyService.updateById(apply);
		return applyNum;
	}
	
	/**
	 * 打招呼
	 * @param wqFriendApply
	 * @return
	 */
	public WqFriendApplyVO addFriendApply(WqFriendApply wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}

	public WqFriendApplyVO addWechat(WqFriendApply wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}
	
	/**
	 * 匹配成功
	 * @param wqFriendApply
	 * @return
	 */
	public WqFriendApplyVO addCardWechat(WqFriendApply wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}
	
	public void updateAgree(WqFriendApply wqFriendApply) {
		// 打招呼需要消耗
		Integer coinNum = 15;
		if(StringUtils.isBlank(wqFriendApply.getMessage())){
			throw new ServiceException("请输入微信号");
		}
		WqFriendApply apply = wqFriendApplyService.getById(wqFriendApply.getId());
		if("1".equals(apply.getStatus())){
			throw new ServiceException("请添加微信交流");
		}
		
		if("0".equals(apply.getStatus())){
			/*if("1".equals(apply.getIsDelete())){
				throw new ServiceException("申请已过期");
			}*/
			
			apply.setStatus("1");
			apply.setContact(wqFriendApply.getMessage());
			apply.setUpdateTime(DateUtils.getNowDate());
			wqFriendApplyService.updateById(apply);
			
			/*PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
    		payCoinUserForm.setUserId(user.getUserId());
    		payCoinUserForm.setCoinNum(coinNum);
    		payCoinUserForm.setCoinType("0");
    		payCoinUserForm.setSourceId(apply.getId());
    		payCoinUserForm.setRemark("被打招呼奖励");
    		payCoinUserService.savePayCoinUser(payCoinUserForm);*/
		}
	}
	
	public void updateJujue(WqFriendApply wqFriendApply) {
		WqFriendApply apply = wqFriendApplyService.getById(wqFriendApply.getId());
		if("0".equals(apply.getStatus())){
			apply.setStatus("2");
			apply.setUpdateTime(DateUtils.getNowDate());
			wqFriendApplyService.updateById(apply);
			
			// 拒绝退还
    		//payCoinUserService.updateCoinUserBack(apply.getApplyUserId(), apply.getId(), "打招呼被拒退还");
		}
	}

	public Integer getLuck() {
		// 卜卦需要消耗
		Integer coinNum = 100;
		Integer amount = payCoinUserService.getAmount();
		if(coinNum > amount){
			// 趣豆不足
			throw new ServiceException("卜卦需要【"+coinNum+"】趣豆，请充值",888);
		}
		
		PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		payCoinUserForm.setUserId(SecurityUtils.getUserId());
		payCoinUserForm.setCoinNum(coinNum);
		payCoinUserForm.setCoinType("1");
		payCoinUserForm.setRemark("卜卦消费");
		payCoinUserService.savePayCoinUser(payCoinUserForm);
		return GoodLuck.luck(null, null, null);
	}

	public boolean adPlay(WqFriendApply wqFriendApply) {
		// 广告奖励
		Integer coinNum = 30;
		PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		payCoinUserForm.setUserId(SecurityUtils.getUserId());
		payCoinUserForm.setCoinNum(coinNum);
		payCoinUserForm.setCoinType("0");
		payCoinUserForm.setRemark("观看广告奖励");
		payCoinUserService.savePayCoinUser(payCoinUserForm);
		return true;
	}

	public AjaxResult addVipWechat(WqFriendApply wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}

	public WqFriendApplyVO addChat(WqFriendApply wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}

	public String getWechat(WqFriendApplyVO wqFriendApply) {
		if(wqFriendApply != null){
			throw new ServiceException("即时通讯请升级商业版");
		}
		return null;
	}
	
}
